package com.cby.operation.pay.channel.alipay

import android.app.Activity
import android.text.TextUtils
import android.util.Log
import com.alipay.sdk.app.PayTask
import com.cby.operation.pay.bean.PayOrderInfo
import com.cby.operation.pay.channel.PayResultCallBack
import com.cby.operation.pay.channel.PayWrapper
import com.cby.operation.sdk.ERR_MSG_SDK_ACTIVITY_IS_NULL

/**
 * 描述信息:支付宝支付包装类
 *
 * @author xuzhiwei
 * @create 2020/8/3
 */
class AlipayWrapper : PayWrapper {
    override fun doPay(
        activity: Activity?, orderInfo: PayOrderInfo?,
        callBack: PayResultCallBack?
    ) {
        val payRunnable = Runnable {
            var alipay: PayTask? = null
            alipay = if (activity == null) {
                //Activity currentActivity = HykbAntiManager.getInstance().getCurrentActivity();
                //alipay = new PayTask(currentActivity);
                callBack!!.onResult(false, "", ERR_MSG_SDK_ACTIVITY_IS_NULL)
                return@Runnable
            } else {
                PayTask(activity)
            }
            val orderStr = orderInfo!!.payParams!!.params
            val result = alipay.payV2(orderStr, true)
            Log.i("msp", result.toString())
            val payResult = AliPayResult(result)
            val resultInfo = payResult.result // 同步返回需要验证的信息
            val resultStatus = payResult.resultStatus
            // 判断resultStatus 为9000则代表支付成功
            val success = TextUtils.equals(resultStatus, HYKB_ALIPAY_CODE_SUCCESS)
            var message = ""
            when (resultStatus) {
                HYKB_ALIPAY_CODE_SUCCESS -> message = HYKB_ALIPAY_MSG_SUCCESS
                HYKB_ALIPAY_CODE_RESULT_CONFIRM -> message = HYKB_ALIPAY_MSG_RESULT_CONFIRM
                HYKB_ALIPAY_CODE_SYSTEM_EXCEPTION -> message = HYKB_ALIPAY_MSG_SYSTEM_EXCEPTION
                HYKB_ALIPAY_CODE_DATA_FORMAT_ERROR -> message = HYKB_ALIPAY_MSG_DATA_FORMAT_ERROR
                HYKB_ALIPAY_CODE_CANCEL_PAY -> message = HYKB_ALIPAY_MSG_CANCEL_PAY
                else -> {
                }
            }
            callBack!!.onResult(success, resultStatus, message)
        }

        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }

    companion object {
        /**
         * 9000, 支付成功
         * 8000, 支付结果确认中
         * 4000, 系统异常
         * 4001, 数据格式不正确
         * 6001, 取消支付操作
         */
        /** 支付成功  */
        private const val HYKB_ALIPAY_CODE_SUCCESS = "9000"

        /** 支付结果确认中  */
        private const val HYKB_ALIPAY_CODE_RESULT_CONFIRM = "8000"

        /** 系统异常  */
        private const val HYKB_ALIPAY_CODE_SYSTEM_EXCEPTION = "4000"

        /** 数据格式不正确  */
        private const val HYKB_ALIPAY_CODE_DATA_FORMAT_ERROR = "4001"

        /** 取消支付  */
        private const val HYKB_ALIPAY_CODE_CANCEL_PAY = "6001"
        private const val HYKB_ALIPAY_MSG_SUCCESS = "支付成功"
        private const val HYKB_ALIPAY_MSG_RESULT_CONFIRM = "支付结果确认中"
        private const val HYKB_ALIPAY_MSG_SYSTEM_EXCEPTION = "系统异常"
        private const val HYKB_ALIPAY_MSG_DATA_FORMAT_ERROR = "数据格式不正确"
        private const val HYKB_ALIPAY_MSG_CANCEL_PAY = "取消支付操作"
    }
}